package com.huanghwh.sync.core.config;

import com.huanghwh.sync.core.service.ITableSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author: huanghwh
 * @Date: 2021/12/24 09:10
 * @Description: 启动时全量数据初始化
 */
@Component
@Slf4j
public class FullSyncInit implements ApplicationRunner {

    @Autowired
    private ITableSyncService tableSyncService;

    @Value("${fullSync.enable}")
    private Boolean fullSync;

    @Value("${fullSync.minute}")
    private Integer fullSyncMinute;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (Boolean.TRUE.equals(fullSync)) {
            log.info("=============全量数据同步-开始=============");
            if (Objects.isNull(fullSyncMinute)) {
                throw new NullPointerException("fullSync.minute is required");
            }
            tableSyncService.startSync(fullSyncMinute);
            log.info("=============全量数据同步-结束=============");
        }
    }
}
